﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKRevitBase.WinForm;
using YJKAreaForDraw;
using YJKElementFinish.ColumnSurface.Forms;
using YRevitJig;
using YJKPresentation.Utils;

namespace YJKArchUtils.ControlUtility
{
	public class SelectAreaNewManager
	{
		public bool IfWinFormClose
		{
			get
			{
				return this.m_bIfWinFormClose;
			}
		}

		public SelectAreaNewManager(ExternalCommandData commandData)
		{
			this.m_Revit = commandData;
			this.m_useApplication = this.m_Revit.Application;
			this.m_Doc = this.m_useApplication.ActiveUIDocument.Document;
		}

		public SelectAreaNewManager(UIApplication inputUIApplication)
		{
			this.m_useApplication = inputUIApplication;
			this.m_Doc = this.m_useApplication.ActiveUIDocument.Document;
		}

		public SelectAreaNewManager(ExternalCommandData commandData, bool bIsCloseBtn)
		{
			this.m_Revit = commandData;
			this.m_uidoc = this.m_Revit.Application.ActiveUIDocument;
			this.m_useApplication = this.m_Revit.Application;
			this.m_Doc = this.m_useApplication.ActiveUIDocument.Document;
			this.m_bIsExistClose = bIsCloseBtn;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result EditOutLine()
		{
			if (this.m_Doc.ActiveView.SketchPlane == null)
			{
				Transaction transaction = null;
				try
				{
					transaction = new Transaction(this.m_Doc);
					transaction.Start("new work plane");
					Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_Doc.ActiveView.ViewDirection, this.m_Doc.ActiveView.Origin);
					SketchPlane sketchPlane = SketchPlane.Create(this.m_Doc, plane);
					this.m_Doc.ActiveView.SketchPlane = sketchPlane;
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction != null)
					{
						transaction.RollBack();
					}
					return Autodesk.Revit.UI.Result.Failed;
				}
			}
			EditingInterfaceForm.FormOperateStyle formOperateStyle = EditingInterfaceForm.FormOperateStyle.FOS_FLOOR;
			EditingInterfaceForm editingInterfaceForm = new EditingInterfaceForm(SelectAreaNewManager._hWndRevit.Handle, formOperateStyle);
			editingInterfaceForm.Show(SelectAreaNewManager._hWndRevit);
			int wMsg = 7;
			SelectAreaNewManager.SendMessage(SelectAreaNewManager._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result = Result.Cancelled;
			try
			{
				for (;;)
				{
					switch (editingInterfaceForm.CurrentOperateType)
					{
					case EditingInterfaceForm.OperateType.OT_DrawLine:
						result = this.DrawLinearOutLine(editingInterfaceForm.IsContinuous, 0);
						break;
					case EditingInterfaceForm.OperateType.OT_DrawRect:
						result = this.DrawRectOutLine(1);
						break;
					case EditingInterfaceForm.OperateType.OT_DrawArc3Pt:
						result = this.DrawArc3PtOutLine(editingInterfaceForm.IsContinuous, 2);
						break;
					case EditingInterfaceForm.OperateType.OT_DrawArcCenter:
						result = this.DrawArcCenterOutLine(3);
						break;
					case EditingInterfaceForm.OperateType.OT_SelectRefCurve:
						result = this.SelectRefCurveOutLine();
						break;
					case EditingInterfaceForm.OperateType.OT_SelectRefFloor:
						result = this.SelectReferenceOutLine();
						break;
					case EditingInterfaceForm.OperateType.OT_Delete:
						result = this.DeleteOutLine();
						break;
					}
					if (result == Autodesk.Revit.UI.Result.Cancelled)
					{
						if (!editingInterfaceForm.PickButton)
						{
							break;
						}
						editingInterfaceForm.PickButton = false;
					}
					else if (result == Autodesk.Revit.UI.Result.Failed)
					{
						break;
					}
				}
				this.ClearTagCurves();
			}
			catch (Exception ex)
			{
				ex.ToString();
				this.ClearTagCurves();
				editingInterfaceForm.Close();
				return Autodesk.Revit.UI.Result.Failed;
			}
			editingInterfaceForm.Close();
			this.m_bIfOk = editingInterfaceForm.IfOk;
			this.m_bIfClose = editingInterfaceForm.IfClose;
			this.m_bIfWinFormClose = editingInterfaceForm.IfWinFormClose;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private string GetstatusPromptWithLine(int index)
		{
			string result = "选择直线轮廓线的起始点";
			if (index % 2 == 1)
			{
				result = "选择直线轮廓线的终点";
			}
			return result;
		}

		private bool IsContinuous(bool isContinuous)
		{
			return isContinuous && this.m_PickLastPoint != null;
		}

		private Result DrawLinearOutLine(bool isContinuous, int nFlag)
		{
			try
			{
				int num = 0;
				if (this.IsContinuous(isContinuous))
				{
					num++;
				}
				string statusPrompt = this.GetstatusPromptWithLine(num);
				XYZ xyz = null;
				XYZ xyz2 = null;
				while (this.PickPoint(statusPrompt, this.m_PickLastPoint, nFlag, ref xyz))
				{
					num++;
					if (xyz2 != null || this.IsContinuous(isContinuous))
					{
						if (this.IsContinuous(isContinuous))
						{
							this.ClearTagCurves();
							xyz2 = this.m_PickLastPoint;
							this.DrawStartPointTag(xyz2);
							num++;
							statusPrompt = this.GetstatusPromptWithLine(1);
						}
						else
						{
							statusPrompt = this.GetstatusPromptWithLine(0);
						}
						List<SelAreaEdgeInfo> list = new List<SelAreaEdgeInfo>();
						SelAreaEdgeInfo item = new SelAreaEdgeInfo(xyz2, xyz, 0.0);
						list.Add(item);
						this.DrawLines(list);
						xyz2 = null;
						if (!this.IsContinuous(isContinuous))
						{
							this.ClearTagCurves();
						}
					}
					else if (num % 2 == 1)
					{
						statusPrompt = this.GetstatusPromptWithLine(1);
						xyz2 = xyz;
						this.DrawStartPointTag(xyz2);
					}
					this.m_PickLastPoint = xyz;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private string GetstatusPromptWithBox(int index)
		{
			string result = "选择轮廓线矩形的起始点";
			if (index % 2 == 1)
			{
				result = "选择轮廓线矩形的终点";
			}
			return result;
		}

		private Result DrawRectOutLine(int nFlag)
		{
			this.m_PickLastPoint = null;
			Transform identity = Transform.Identity;
			identity.Origin = this.m_Doc.ActiveView.Origin;
			identity.BasisX = this.m_Doc.ActiveView.RightDirection;
			identity.BasisY = this.m_Doc.ActiveView.UpDirection;
			identity.BasisZ = this.m_Doc.ActiveView.ViewDirection;
			Transform inverse = identity.Inverse;
			try
			{
				int num = 0;
				string statusPrompt = this.GetstatusPromptWithBox(num);
				XYZ xyz = null;
				XYZ xyz2 = null;
				while (this.PickPoint(statusPrompt, xyz2, nFlag, ref xyz))
				{
					num++;
					statusPrompt = this.GetstatusPromptWithBox(num);
					if (num % 2 == 1)
					{
						xyz2 = xyz;
						this.DrawStartPointTag(xyz2);
					}
					else if (xyz2 != null)
					{
						XYZ xyz3 = inverse.OfPoint(xyz2);
						XYZ xyz4 = inverse.OfPoint(xyz);
						double x = xyz3.X;
						double x2 = xyz4.X;
						double y = xyz3.Y;
						double y2 = xyz4.Y;
						if (Geometry.LessThan(x2, x))
						{
							x = xyz4.X;
							x2 = xyz3.X;
						}
						if (Geometry.LessThan(y2, y))
						{
							y = xyz4.Y;
							y2 = xyz3.Y;
						}
						XYZ xyz5 = new XYZ(x, y, 0.0);
						XYZ xyz6 = new XYZ(x2, y, 0.0);
						XYZ xyz7 = new XYZ(x2, y2, 0.0);
						XYZ xyz8 = new XYZ(x, y2, 0.0);
						xyz5 = identity.OfPoint(xyz5);
						xyz6 = identity.OfPoint(xyz6);
						xyz7 = identity.OfPoint(xyz7);
						xyz8 = identity.OfPoint(xyz8);
						List<SelAreaEdgeInfo> list = new List<SelAreaEdgeInfo>();
						SelAreaEdgeInfo item = new SelAreaEdgeInfo(xyz5, xyz6, 0.0);
						list.Add(item);
						SelAreaEdgeInfo item2 = new SelAreaEdgeInfo(xyz6, xyz7, 0.0);
						list.Add(item2);
						SelAreaEdgeInfo item3 = new SelAreaEdgeInfo(xyz7, xyz8, 0.0);
						list.Add(item3);
						SelAreaEdgeInfo item4 = new SelAreaEdgeInfo(xyz8, xyz5, 0.0);
						list.Add(item4);
						this.DrawLines(list);
						xyz2 = null;
						this.ClearTagCurves();
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private string GetstatusPromptWithArc3Pt(int index)
		{
			string result = "选择弧形轮廓线的起始点";
			if (index % 3 == 1)
			{
				result = "选择弧形轮廓线的终点";
			}
			else if (index % 3 == 2)
			{
				result = "选择弧形轮廓线的线上点";
			}
			return result;
		}

		private Result DrawArc3PtOutLine(bool isContinuous, int nFlag)
		{
			try
			{
				int num = 0;
				if (this.IsContinuous(isContinuous))
				{
					num++;
				}
				string statusPrompt = this.GetstatusPromptWithArc3Pt(num);
				XYZ xyz = null;
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				while (this.PickPoint(statusPrompt, xyz2, xyz3, nFlag, ref xyz))
				{
					num++;
					statusPrompt = this.GetstatusPromptWithArc3Pt(num);
					if (xyz2 != null && xyz3 != null)
					{
						if (Geometry.Is_Points_Collinear(xyz2, xyz3, xyz))
						{
							YJKMessageBox.Show("三点不能共线");
							num--;
							statusPrompt = this.GetstatusPromptWithArc3Pt(num);
						}
						else
						{
							this.DrawLines(new List<Curve>
							{
								Arc.Create(xyz2, xyz3, xyz)
							}, true);
							xyz2 = this.m_PickLastPoint;
							xyz3 = null;
							this.ClearTagCurves();
						}
					}
					else if (num % 3 == 2 || this.IsContinuous(isContinuous))
					{
						if (this.IsContinuous(isContinuous))
						{
							this.ClearTagCurves();
							xyz2 = this.m_PickLastPoint;
							this.DrawStartPointTag(xyz2);
							num++;
						}
						xyz3 = xyz;
						this.DrawStartPointTag(xyz3);
						this.m_PickLastPoint = xyz;
					}
					else if (num % 3 == 1)
					{
						xyz2 = xyz;
						this.DrawStartPointTag(xyz2);
						this.m_PickLastPoint = xyz;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private string GetstatusPromptWithArcCenter(int index)
		{
			string result = "选择弧形轮廓线的圆心";
			if (index % 3 == 1)
			{
				result = "选择弧形轮廓线的起始点";
			}
			else if (index % 3 == 2)
			{
				result = "选择弧形轮廓线的终点";
			}
			return result;
		}

		private Result DrawArcCenterOutLine(int nFlag)
		{
			this.m_PickLastPoint = null;
			try
			{
				int num = 0;
				string statusPrompt = this.GetstatusPromptWithArcCenter(num);
				XYZ xyz = null;
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				while (this.PickPoint(statusPrompt, xyz3, xyz2, nFlag, ref xyz))
				{
					num++;
					statusPrompt = this.GetstatusPromptWithArcCenter(num);
					if (num % 3 == 1)
					{
						xyz2 = xyz;
						this.DrawStartPointTag(xyz2);
					}
					else if (num % 3 == 2)
					{
						xyz3 = xyz;
						this.DrawStartPointTag(xyz3);
					}
					else if (xyz2 != null && xyz3 != null)
					{
						double num2 = xyz2.DistanceTo(xyz3);
						XYZ xyz4 = xyz2 + (xyz - xyz2).Normalize() * num2;
						double angle = Geometry.getAngle(xyz2, xyz3, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
						double angle2 = Geometry.getAngle(xyz2, xyz4, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
						double num3 = Geometry.BetweenTheAngles(angle, angle2, true);
						if (Geometry.IsEqual(num3, 0.0, 0.0001))
						{
							YJKMessageBox.Show("转角不能为零");
							num--;
							statusPrompt = this.GetstatusPromptWithArcCenter(num);
						}
						else
						{
							XYZ xyz5 = Geometry.RotateTo(xyz3 - xyz2, num3 / 2.0, this.m_Doc.ActiveView.ViewDirection);
                            //xyz2 + xyz5;
							this.DrawLines(new List<Curve>
							{
								this.CreateArc(xyz2, xyz3, xyz4)
							}, true);
							xyz2 = null;
							xyz3 = null;
							this.ClearTagCurves();
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private Arc CreateArc(XYZ ptCenter, XYZ ptArcStart, XYZ ptArcEnd)
		{
			double length = (ptArcStart - ptCenter).GetLength();
			XYZ xyz = ptArcStart - ptCenter;
			XYZ xyz2 = ptArcEnd - ptCenter;
			xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
			double num;
			double num2;
			if (xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ) <= Math.PI)
			{
				num = XYZ.BasisX.AngleOnPlaneTo(xyz, XYZ.BasisZ);
				num2 = XYZ.BasisX.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
				if (num > num2)
				{
					num2 += Math.PI*2;
				}
				return Arc.Create(ptCenter, length, num, num2, XYZ.BasisX, XYZ.BasisY);
			}
			num = XYZ.BasisX.Negate().AngleOnPlaneTo(xyz, XYZ.BasisZ.Negate());
			num2 = XYZ.BasisX.Negate().AngleOnPlaneTo(xyz2, XYZ.BasisZ.Negate());
			if (num > num2)
			{
				num2 += Math.PI*2;
			}
			return Arc.Create(ptCenter, length, num, num2, XYZ.BasisX.Negate(), XYZ.BasisY);
		}

		private Result SelectRefCurveOutLine()
		{
			this.m_PickLastPoint = null;
			Transform identity = Transform.Identity;
			identity.Origin = this.m_Doc.ActiveView.Origin;
			identity.BasisX = this.m_Doc.ActiveView.RightDirection;
			identity.BasisY = this.m_Doc.ActiveView.UpDirection;
			identity.BasisZ = this.m_Doc.ActiveView.ViewDirection;
			Transform inverse = identity.Inverse;
			try
			{
				Curve curve = null;
				while (this.PickRefCurve(ref curve))
				{
					XYZ xyz = curve.GetEndPoint(0);
					xyz = inverse.OfPoint(xyz);
					xyz = identity.OfPoint(new XYZ(xyz.X, xyz.Y, 0.0));
					XYZ xyz2 = curve.GetEndPoint(1);
					xyz2 = inverse.OfPoint(xyz2);
					xyz2 = identity.OfPoint(new XYZ(xyz2.X, xyz2.Y, 0.0));
					if (!Geometry.IsEqual(xyz.DistanceTo(xyz2), 0.0))
					{
						if (curve.GetType() == typeof(Line))
						{
							this.DrawLines(new List<Curve>
							{
								Line.CreateBound(xyz, xyz2)
							}, true);
						}
						else if (curve.GetType() == typeof(Arc))
						{
							Arc arc = curve as Arc;
							XYZ xyz3 = arc.Center;
							xyz3 = inverse.OfPoint(xyz3);
							xyz3 = identity.OfPoint(new XYZ(xyz3.X, xyz3.Y, 0.0));
							if (!Geometry.IsEqual(arc.Normal, this.m_Doc.ActiveView.ViewDirection))
							{
								XYZ xyz4 = xyz;
								xyz = xyz2;
								xyz2 = xyz4;
							}
							double radius = arc.Radius;
							double angle = Geometry.getAngle(xyz3, xyz, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
							double angle2 = Geometry.getAngle(xyz3, xyz2, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
							double num = Geometry.BetweenTheAngles(angle, angle2, true);
							XYZ xyz5 = Geometry.RotateTo(xyz - xyz3, num / 2.0, this.m_Doc.ActiveView.ViewDirection);
							XYZ xyz6 = xyz3 + xyz5;
							this.DrawLines(new List<Curve>
							{
								Arc.Create(xyz, xyz2, xyz6)
							}, true);
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private Result SelectReferenceOutLine()
		{
			this.m_PickLastPoint = null;
			Transform identity = Transform.Identity;
			identity.Origin = this.m_Doc.ActiveView.Origin;
			identity.BasisX = this.m_Doc.ActiveView.RightDirection;
			identity.BasisY = this.m_Doc.ActiveView.UpDirection;
			identity.BasisZ = this.m_Doc.ActiveView.ViewDirection;
			Transform inverse = identity.Inverse;
			try
			{
				Curve curve = null;
				while (this.PickEdge(ref curve))
				{
					XYZ xyz = curve.GetEndPoint(0);
					xyz = inverse.OfPoint(xyz);
					xyz = identity.OfPoint(new XYZ(xyz.X, xyz.Y, 0.0));
					XYZ xyz2 = curve.GetEndPoint(1);
					xyz2 = inverse.OfPoint(xyz2);
					xyz2 = identity.OfPoint(new XYZ(xyz2.X, xyz2.Y, 0.0));
					if (!Geometry.IsEqual(xyz.DistanceTo(xyz2), 0.0))
					{
						if (curve.GetType() == typeof(Line))
						{
							this.DrawLines(new List<Curve>
							{
								Line.CreateBound(xyz, xyz2)
							}, true);
						}
						else if (curve.GetType() == typeof(Arc))
						{
							Arc arc = curve as Arc;
							XYZ xyz3 = arc.Center;
							xyz3 = inverse.OfPoint(xyz3);
							xyz3 = identity.OfPoint(new XYZ(xyz3.X, xyz3.Y, 0.0));
							if (!Geometry.IsEqual(arc.Normal, this.m_Doc.ActiveView.ViewDirection))
							{
								XYZ xyz4 = xyz;
								xyz = xyz2;
								xyz2 = xyz4;
							}
							double radius = arc.Radius;
							double angle = Geometry.getAngle(xyz3, xyz, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
							double angle2 = Geometry.getAngle(xyz3, xyz2, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.ViewDirection);
							double num = Geometry.BetweenTheAngles(angle, angle2, true);
							XYZ xyz5 = Geometry.RotateTo(xyz - xyz3, num / 2.0, this.m_Doc.ActiveView.ViewDirection);
							XYZ xyz6 = xyz3 + xyz5;
							this.DrawLines(new List<Curve>
							{
								Arc.Create(xyz, xyz2, xyz6)
							}, true);
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private void ClearTagCurves()
		{
			if (this.m_TagCurves.Count == 0)
			{
				return;
			}
			Transaction transaction = new Transaction(this.m_Doc);
			try
			{
				transaction.Start("delete tag curves");
				this.m_Doc.Delete(this.m_TagCurves);
				transaction.Commit();
				this.m_TagCurves.Clear();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
			}
		}

		private Result DeleteOutLine()
		{
			try
			{
				List<Element> list = new List<Element>();
				while (this.PickOutLine(ref list))
				{
					Transaction transaction = new Transaction(this.m_Doc);
					transaction.Start("delete outLine");
					foreach (Element element in list)
					{
						try
						{
							this.m_Doc.Delete(element.Id);
							this.m_lstDetailCurveId.Remove(element.Id);
						}
						catch (Exception)
						{
						}
					}
					transaction.Commit();
					this.m_lstDetailCurveId.Clear();
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		private bool PickRefCurve(ref Curve outLineCurve)
		{
			Selection selection = this.m_useApplication.ActiveUIDocument.Selection;
			try
			{
				RefCurveFilter refCurveFilter = new RefCurveFilter();
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, refCurveFilter, "拾取详图线生成轮廓线");
				DetailCurve detailCurve = this.m_Doc.GetElement(reference) as DetailCurve;
				this.m_LineStyleOld = (detailCurve.LineStyle as GraphicsStyle);
				outLineCurve = detailCurve.GeometryCurve;
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickOutLine(ref List<Element> outLineList)
		{
			Selection selection = this.m_useApplication.ActiveUIDocument.Selection;
			try
			{
				OutLineFilter outLineFilter = new OutLineFilter(false);
				foreach (Element element in selection.PickElementsByRectangle(outLineFilter, "选择轮廓线"))
				{
					DetailCurve item = element as DetailCurve;
					outLineList.Add(item);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickPoint(string statusPrompt, ref XYZ ptPick)
		{
			try
			{
				ptPick = this.m_useApplication.ActiveUIDocument.Selection.PickPoint(statusPrompt);
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickPoint(string statusPrompt, XYZ ptStart, int nFlag, ref XYZ ptPick)
		{
			try
			{
				if (ptStart == null)
				{
					ptPick = this.m_useApplication.ActiveUIDocument.Selection.PickPoint(statusPrompt);
				}
				else if (nFlag == 0)
				{
					ptPick = RevitExtensionAPI.PickOtherPoint(ptStart, this.m_uidoc, statusPrompt);
				}
				else if (nFlag == 1)
				{
					ptPick = RevitExtensionAPI.PickRectOtherPoint(ptStart, this.m_uidoc, statusPrompt);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickPoint(string statusPrompt, XYZ ptStart, XYZ ptEnd, int nFlag, ref XYZ ptPick)
		{
			try
			{
				if (ptStart == null)
				{
					ptPick = this.m_useApplication.ActiveUIDocument.Selection.PickPoint(statusPrompt);
				}
				else if (ptEnd == null)
				{
					ptPick = RevitExtensionAPI.PickOtherPoint(ptStart, this.m_uidoc, statusPrompt);
				}
				else if (nFlag == 2)
				{
					ptPick = RevitExtensionAPI.PickArcMidPoint(ptStart, ptEnd, this.m_uidoc, statusPrompt);
				}
				else if (nFlag == 3)
				{
					ptPick = RevitExtensionAPI.PickArcEndPoint(ptStart, ptEnd, this.m_uidoc, statusPrompt);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickBox(string statusPrompt, ref XYZ ptMin, ref XYZ ptMax)
		{
			try
			{
				PickedBox pickedBox = this.m_useApplication.ActiveUIDocument.Selection.PickBox((Autodesk.Revit.UI.Selection.PickBoxStyle)0, statusPrompt);
				ptMin = pickedBox.Min;
				ptMax = pickedBox.Max;
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			return true;
		}

		private bool PickEdge(ref Curve refCurve)
		{
			Reference reference = null;
			Selection selection = this.m_useApplication.ActiveUIDocument.Selection;
			try
			{
				EdgeFilter edgeFilter = new EdgeFilter(this.m_Doc);
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)4, edgeFilter, "选择墙边界生成轮廓线");
			}
			catch (Exception)
			{
				return false;
			}
			if (reference == null)
			{
				return false;
			}
			Face face = this.m_Doc.GetElement(reference).GetGeometryObjectFromReference(reference) as Face;
			if (null == face)
			{
				return false;
			}
			XYZ globalPoint = reference.GlobalPoint;
			double val = double.MaxValue;
			Curve curve = null;
			foreach (object obj in face.EdgeLoops)
			{
				foreach (object obj2 in ((EdgeArray)obj))
				{
					Curve curve2 = ((Edge)obj2).AsCurve();
					XYZ endPoint = curve2.GetEndPoint(0);
					XYZ endPoint2 = curve2.GetEndPoint(1);
					if (Geometry.IsEqual(endPoint.Z, endPoint2.Z))
					{
						double num = curve2.Distance(new XYZ(globalPoint.X, globalPoint.Y, endPoint.Z));
						if (Geometry.LessThan(num, val))
						{
							val = num;
							curve = curve2;
						}
					}
				}
			}
			if (curve == null)
			{
				return false;
			}
			refCurve = curve;
			return true;
		}

		public void DrawStartPointTag(XYZ pos)
		{
			this.ClearTagCurves();
			CurveArray curveArray = new CurveArray();
			double num = AssistFunc.mmToFeet(300.0);
			Autodesk.Revit.DB.View activeView = this.m_Doc.ActiveView;
			XYZ startPoint = pos + -num * activeView.RightDirection + num * activeView.UpDirection;
			XYZ endPoint = pos + num * activeView.RightDirection + -num * activeView.UpDirection;
			XYZ startPoint2 = pos + -num * activeView.RightDirection + -num * activeView.UpDirection;
			XYZ endPoint2 = pos + num * activeView.RightDirection + num * activeView.UpDirection;
			Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
			curveArray.Append(line);
			Line line2 = YJKLineEx.YJKGetBound(startPoint2, endPoint2);
			curveArray.Append(line2);
			Transaction transaction = new Transaction(this.m_Doc);
			try
			{
				transaction.Start("new tag curves");
				foreach (object obj in this.m_Doc.Create.NewDetailCurveArray(this.m_Doc.ActiveView, curveArray))
				{
					DetailCurve detailCurve = (DetailCurve)obj;
					this.m_TagCurves.Add(detailCurve.Id);
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
			}
		}

		private void DrawLines(List<SelAreaEdgeInfo> lines)
		{
			List<Element> list = new List<Element>();
			this.DrawLines(lines, ref list);
			foreach (Element element in list)
			{
				DetailCurve detailCurve = element as DetailCurve;
				if (detailCurve != null)
				{
					this.AddSelAreaDetaiCurveId(detailCurve);
				}
			}
		}

		private void DrawLines(List<SelAreaEdgeInfo> lines, ref List<Element> linesList)
		{
			if (lines.Count == 0)
			{
				return;
			}
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.m_Doc);
				transaction.Start("draw building outline");
				UIApplication useApplication = this.m_useApplication;
				CurveArray curveArray = new CurveArray();
				foreach (SelAreaEdgeInfo selAreaEdgeInfo in lines)
				{
					XYZ xyz = selAreaEdgeInfo.StartPoint;
					if (Geometry.LessThan(xyz.Z, 0.0))
					{
						xyz = new XYZ(xyz.X, xyz.Y, 0.0);
					}
					XYZ xyz2 = selAreaEdgeInfo.EndPoint;
					if (Geometry.LessThan(xyz2.Z, 0.0))
					{
						xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
					}
					if (!Geometry.IsEqual(xyz.DistanceTo(xyz2), 0.0))
					{
						curveArray.Append(Line.CreateBound(xyz, xyz2));
					}
				}
				DetailCurveArray detailCurveArray = this.m_Doc.Create.NewDetailCurveArray(this.m_Doc.ActiveView, curveArray);
				if (this.GetLineStyle())
				{
					List<ElementId> list = new List<ElementId>();
					foreach (object obj in detailCurveArray)
					{
						DetailCurve detailCurve = (DetailCurve)obj;
						detailCurve.LineStyle = SelectAreaNewManager.m_LineStyle;
						linesList.Add(detailCurve);
						list.Add(detailCurve.Id);
					}
					LinePatternElement linePatternElementByName = LinePatternElement.GetLinePatternElementByName(this.m_Doc, SelectAreaNewManager.m_strLinePatternName);
					if (linePatternElementByName != null)
					{
						this.m_Doc.ActiveView.SetYJKProjLinePatternOverrideByElement(list, linePatternElementByName);
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction != null)
				{
					transaction.RollBack();
				}
			}
		}

		private void DrawLines(List<Curve> lines, bool bAdd2Array = false)
		{
			if (lines.Count == 0)
			{
				return;
			}
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.m_Doc);
				transaction.Start("draw building outline");
				UIApplication useApplication = this.m_useApplication;
				CurveArray curveArray = new CurveArray();
				foreach (Curve curve in lines)
				{
					curveArray.Append(curve);
				}
				DetailCurveArray detailCurveArray = this.m_Doc.Create.NewDetailCurveArray(this.m_Doc.ActiveView, curveArray);
				if (this.GetLineStyle())
				{
					List<ElementId> list = new List<ElementId>();
					foreach (object obj in detailCurveArray)
					{
						DetailCurve detailCurve = (DetailCurve)obj;
						detailCurve.LineStyle = SelectAreaNewManager.m_LineStyle;
						list.Add(detailCurve.Id);
					}
					LinePatternElement linePatternElementByName = LinePatternElement.GetLinePatternElementByName(this.m_Doc, SelectAreaNewManager.m_strLinePatternName);
					if (linePatternElementByName != null)
					{
						this.m_Doc.ActiveView.SetYJKProjLinePatternOverrideByElement(list, linePatternElementByName);
					}
					if (bAdd2Array)
					{
						this.m_lstDetailCurveId.AddRange(list);
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction != null)
				{
					transaction.RollBack();
				}
			}
		}

		private bool GetLineStyle()
		{
			Category category = this.m_Doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return false;
			}
			int nLineWidth = SelectAreaNewManager.m_nLineWidth;
			string text = SelectAreaNewManager.m_StrLineNamePrefix + nLineWidth.ToString();
            Autodesk.Revit.DB.Color lineColor = new Autodesk.Revit.DB.Color(byte.MaxValue, 0, byte.MaxValue);
			if (!category.SubCategories.Contains(text))
			{
				if (!this.CreateLineStyle(nLineWidth, text, lineColor, ref category))
				{
					return false;
				}
			}
			else
			{
				Category category2 = category.SubCategories.get_Item(text);
				category2.LineColor = lineColor;
				category2.SetLineWeight(nLineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
			}
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null && graphicsStyle.Name.CompareTo(text) == 0)
				{
					SelectAreaNewManager.m_LineStyle = graphicsStyle;
				}
			}
			return SelectAreaNewManager.m_LineStyle != null;
		}

        private bool CreateLineStyle(int lineWidth, string typeName, Autodesk.Revit.DB.Color lineColor, ref Category lineCategory)
		{
			bool result;
			try
			{
				Category category = this.m_Doc.Settings.Categories.NewSubcategory(lineCategory, typeName);
				category.LineColor = lineColor;
				category.SetLineWeight(lineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void AddSelAreaDetaiCurveId(DetailCurve dLine)
		{
			if (!this.m_lstDetailCurveId.Contains(dLine.Id))
			{
				this.m_lstDetailCurveId.Add(dLine.Id);
			}
		}

		private ExternalCommandData m_Revit;

		private UIApplication m_useApplication;

		private Document m_Doc;

		private bool m_bIsExistClose = true;

		private bool m_bIfOk;

		private bool m_bIfClose;

		private bool m_bIfWinFormClose;

		private static GraphicsStyle m_LineStyle = null;

		public static string m_StrLineNamePrefix = "HYProfileLine";

		public List<ElementId> m_lstDetailCurveId = new List<ElementId>();

		public static WindowHandle _hWndRevit = null;

		private List<ElementId> m_TagCurves = new List<ElementId>();

		private static string m_strLinePatternName = "实线";

		private static int m_nLineWidth = 3;

		private XYZ m_PickLastPoint;

		private UIDocument m_uidoc;

		public GraphicsStyle m_LineStyleOld;
	}
}
